﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;



namespace Budget
{
    class BudgetFunctions
    {
        public void SaveComboBoxToXmlFile(BindingList<string> i_listToSave, string i_listName)
        {
            var x = new XmlSerializer(typeof(BindingList<string>));
            var fs = new FileStream(System.Environment.CurrentDirectory + "\\DataFiles\\" + i_listName + ".xml", FileMode.Create);
            x.Serialize(fs, i_listToSave);
            fs.Close();
        }

        public BindingList<string> LoadComboBoxFromXmlFile(string i_listName)
        {
            var x = new XmlSerializer(typeof(BindingList<string>));
            var fs = new System.IO.FileStream(System.Environment.CurrentDirectory + "\\DataFiles\\" + i_listName + ".xml", System.IO.FileMode.Open);
            var fromFile = x.Deserialize(fs) as BindingList<string>;
            fs.Close();
            return fromFile;
        }

        public void AutoCompleteText(ref TextBox i_textBox,DataTable i_table,string i_autoCompleteColumn)
        {
            i_textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            i_textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;

            AutoCompleteStringCollection autoCompleteList = new AutoCompleteStringCollection();

            foreach (DataRow row in i_table.Rows)
            {
                autoCompleteList.Add(row[i_autoCompleteColumn].ToString());
            }

            i_textBox.AutoCompleteCustomSource = autoCompleteList;
        }

        public void UpdateComboboxValues(ref BindingList<string> i_bindingList,string i_xmlFileName,ref ComboBox i_comboBox,string i_defaultValue)
        {
            i_bindingList = new BindingList<string>();
            i_bindingList = this.LoadComboBoxFromXmlFile(i_xmlFileName);
            i_comboBox.DataSource = i_bindingList;
            i_comboBox.SelectedIndex = i_comboBox.Items.IndexOf(i_defaultValue);
        }
    }

    public partial class InputBox
    {
        public static DialogResult Show(string i_Title, string i_PromptText, ref string i_Value)
        {
            Form form = new Form();
            Label label = new Label();
            TextBox textBox = new TextBox();
            Button buttonOK = new Button();
            Button buttonCancel = new Button();

            form.Text = i_Title;
            label.Text = i_PromptText;
            textBox.Text = i_Value;

            buttonOK.Text = "OK";
            buttonCancel.Text = "Cancel";
            buttonOK.DialogResult = DialogResult.OK;
            buttonCancel.DialogResult = DialogResult.Cancel;

            label.SetBounds(9, 20, 372, 13);
            textBox.SetBounds(12, 36, 372, 20);
            buttonOK.SetBounds(228, 72, 75, 23);
            buttonCancel.SetBounds(309, 72, 75, 23);

            label.AutoSize = true;
            textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
            buttonOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            form.ClientSize = new Size(396, 107);
            form.Controls.AddRange(new Control[] { label, textBox, buttonOK, buttonCancel });
            form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.AcceptButton = buttonOK;
            form.CancelButton = buttonCancel;

            DialogResult dialogResult = form.ShowDialog();
            i_Value = textBox.Text;
            return dialogResult;
        }
    }
}
